//
//  CLLSegmentView.m
//  LOL视频播放
//
//  Created by 花花蔡 on 2016/12/5.
//  Copyright © 2016年 com.HuaHuaCai. All rights reserved.
//

#import "CLLSegmentView.h"
#import "Public.h"

@interface CLLSegmentView ()<UIScrollViewDelegate>

//导航按钮条
@property(nonatomic,strong) UIView *segmentView;
//四个控制器
@property(nonatomic,strong) NSArray *controllers;
//btn名字
@property(nonatomic,strong) NSArray *buttonNames;
//四个控制器
@property(nonatomic,strong) NSArray *controllersArray;
//父视图
@property(nonatomic,strong) UIViewController *parentVC;

//btn下面的笑点
@property(nonatomic,strong) UIView *view;
//放四个控制器的滑动视图
@property(nonatomic,strong) UIScrollView *scrollView;
//记录选中的btn是谁
@property(nonatomic,strong) UIButton *selectedBtn;


@end
#define VIEW_W_H 3
#define VIEW_X  (screen_width/4 - 4)/2
#define VIEW_Y CGRectGetMaxY(self.selectedBtn.frame) - 6
@implementation CLLSegmentView

-(instancetype)initWithFrame:(CGRect)frame buttonNames:(NSArray *)buttonNames Controllers:(NSArray *)controllers ParentController:(UIViewController *)parentVC{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.buttonNames = buttonNames;
        self.controllersArray = controllers;
        self.parentVC = parentVC;
        
        [self setupUI];
        
    }
    return self;
}


#pragma mark - setUI
- (void)setupUI{
    NSLog(@"333");
    //创建segmentView
    self.segmentView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, screen_width, 48 * KWidth_ScaleW)];
    self.segmentView.backgroundColor = [UIColor whiteColor];
    self.segmentView.tag = 50;
    [self addSubview:self.segmentView];
    [self addBtnWithBtnName];
    [self addView];
    //加载滑动视图
    [self addScrollView];
    
    //加载四个视图控制器
    [self addControllers];
    
}
#pragma mark - 加载四个视图控制器
- (void)addControllers{
    // 创建ViewController并添加到segmentScrollV
    
    for (int index = 0; index < self.controllersArray.count; index++) {
        UIViewController *controller = self.controllersArray[index];
        controller.view.frame = CGRectMake(index *screen_width, 0, screen_width, self.scrollView.frame.size.height);
        [self.scrollView addSubview:controller.view];
        [_parentVC addChildViewController:controller];
        [controller didMoveToParentViewController:_parentVC];
    }
}

#pragma mark - 加载滑动视图
- (void)addScrollView {
    
    CGFloat scrollViewTopView = CGRectGetMaxY(self.segmentView.frame);
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, scrollViewTopView, screen_width,screen_height - scrollViewTopView - 44)];
    _scrollView.backgroundColor = [UIColor whiteColor];
    //设置滚动范围
    self.scrollView.contentSize = CGSizeMake(screen_width * self.controllersArray.count, 0);
    //设置按页滚动
    self.scrollView.pagingEnabled = YES;
    //设置弹簧效果
    self.scrollView.bounces = YES;
    //隐藏滑动指示器
    self.scrollView.showsVerticalScrollIndicator = NO;
    //设置代理
    self.scrollView.delegate = self ;
    [self addSubview:self.scrollView];
}
#pragma mark - 滑动视图的代理方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat viewX = self.scrollView.contentOffset.x / screen_width * self.selectedBtn.frame.size.width + VIEW_X;
    _view.frame = CGRectMake(viewX, VIEW_Y, VIEW_W_H, VIEW_W_H);
}
//减速结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
//    if (self.scrollView.contentOffset.x > screen_width/2) {
    
//        UIButton *btn = (UIButton *)[self.segmentView viewWithTag:self.scrollView.contentOffset.x / screen_width];
//        
//        self.selectedBtn.selected = NO;
//        self.selectedBtn.titleLabel.font = [UIFont systemFontOfSize:20 * KWidth_ScaleW];
//        
//        self.selectedBtn = btn;
//        self.selectedBtn.selected = YES;
//        self.selectedBtn.titleLabel.font = [UIFont systemFontOfSize:23 * KWidth_ScaleW];
    
        
//    }
    
        CGFloat offsetX = scrollView.contentOffset.x;
        NSInteger btnTag = offsetX / screen_width;
    
        UIButton *btn = (UIButton *)[self.segmentView viewWithTag:btnTag ];
        [self clicked:btn];
    
}
#pragma mark - 加载四个按钮
- (void)addBtnWithBtnName {
    
    for (int index = 0; index < self.buttonNames.count; index ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = index ;
        
        button.frame = CGRectMake(index * (screen_width/self.buttonNames.count), 0, screen_width/self.buttonNames.count, 48 * KWidth_ScaleW);
        [button setTitle:self.buttonNames[index] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        
        button.titleLabel.font = [UIFont systemFontOfSize:20 * KWidth_ScaleW];
        [button addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
        
        if (index == 0) {
            button.selected = YES;
            button.titleLabel.font = [UIFont systemFontOfSize:23 * KWidth_ScaleW];
            self.selectedBtn = button;
        }else
            button.selected = NO;
        
        [self.segmentView addSubview:button];
    }
}
#pragma mark - btn 点击事件
- (void)clicked:(UIButton *)sender {
    
    self.selectedBtn.titleLabel.font = [UIFont systemFontOfSize:20  *KWidth_ScaleW];
    self.selectedBtn.selected = NO;
    self.selectedBtn = sender;
    self.selectedBtn.tag = sender.tag;
    self.selectedBtn.selected = YES;
    self.selectedBtn.titleLabel.font = [UIFont systemFontOfSize:23 * KWidth_ScaleW];
    //小点动画
    [UIView animateWithDuration:0.1 animations:^{
        _view.frame = CGRectMake(VIEW_X + sender.frame.size.width * sender.tag, VIEW_Y , VIEW_W_H, VIEW_W_H);
    }];
    
    //点击按钮后滑动视图滑到相应的视图
    [self.scrollView setContentOffset:CGPointMake(sender.tag * screen_width, 0) animated:YES];
    
}

#pragma mark - 加载指示小红点
- (void)addView {
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(VIEW_X, VIEW_Y, VIEW_W_H, VIEW_W_H)];
    view.layer.cornerRadius = 2;
    view.backgroundColor = [UIColor redColor];
    _view = view;
    //_view.tag = 100;
    [self.segmentView addSubview:view];
}

@end
